import QtQuick 2.0
import "GameLogic.js" as Logic
import "Constants.js" as Constants

Item {
    id: game;
    width: 320;
    height: 240;

    //This is a main full-screen menu. It is shown at the game start
    Rectangle {
        id: mainMenu
        anchors.fill: parent
        color: "black"
        z:10

        Button {
            width: 170
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.verticalCenterOffset: -30
            text: "Start Game"
            onClicked: {
                mainMenu.visible = false
                Logic.initGame();
            }
        }
        Button {
            width: 120
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.verticalCenterOffset: 30
            text: "Quit"
            onClicked: {
                Qt.quit()
            }
        }
    }

    Rectangle {
        id: winScreen
        anchors.fill: parent
        color: "black"
        property alias text: caption.text
        z: 10
        Text{
            id: caption
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            font.pixelSize: Constants.cellSize
            anchors.topMargin: 30
            color: "white"
        }
        visible: false

        Button {
            id: restartButton
            width: 200
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: caption.bottom
            text: "Restart Game"
            anchors.topMargin: 10
            onClicked: {
                console.debug("Re-init game")
                Logic.initGame();
                winScreen.visible = false
            }
        }
        Button {
            id: quitButton
            width: 120
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: restartButton.bottom
            text: "Quit"
            anchors.topMargin: 10
            onClicked: {
                Qt.quit()
            }
        }
    }

    //This is a "next player turn" screen
    Rectangle {
        property alias text: nextTurnCaption.text
        id: nextTurnScreen
        anchors.fill: parent
        opacity: 0.9
        color: "black"
        z:9
        Text {
            id: nextTurnCaption
            anchors.centerIn: parent
            font.pixelSize: Constants.cellSize
            text: "Player turn!"
            color: "lightgrey"
        }
        MouseArea{
            anchors.fill: parent
            onClicked:{
                parent.visible = false
            }
        }
    }

    //This is used to draw background image for the entire game screen
    Rectangle {
        id: bg;
        z:0
        anchors.fill: parent;

        //This defines battle map and should load different tiles for each cell
        //It is also used to contain all the units
        Item {
            id: field
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.margins: 5
            width: Constants.maxCol * Constants.cellSize;
            height: Constants.maxRow * Constants.cellSize;

            //This is used to draw context menu above all units
            Item {
                id: fieldCanvas
                z:50
                anchors.fill: parent

                //The context menu itself
                UnitMenu {
                    id: contextMenu
                    x: 0
                    y: 0
                    z: 10
                    visible: false
                    onShowMoves: {
                        if(Logic.units[ownerIndex].moved != true){
                            Logic.drawSelectors(ownerIndex, "move")
                        }
                    }
                    onShowThrows: {
                        if(Logic.units[ownerIndex].thrown!= true){
                            Logic.drawSelectors(ownerIndex, "throw")
                        }
                    }
                    onShowSpecials: {
                        if(Logic.units[ownerIndex].speced != true){
                            Logic.drawSelectors(ownerIndex, "special")
                        }
                    }
                }

                //This is used to handle all battle map interactions
                MouseArea{
                    id: clickArea
                    z: 5
                    anchors.fill: parent
                    onClicked: {
                        Logic.handleClick(mouseX, mouseY);
                    }
                }
            }
        }
        Button {
            text: "->"
            anchors.right: parent.right
            anchors.left: field.right
            anchors.top: parent.top
            anchors.margins: 5
            onClicked: {
                Logic.nextTurn()
            }
        }
        Button {
            text: "Quit"
            anchors.right: parent.right
            anchors.left: field.right
            anchors.bottom: parent.bottom
            anchors.margins: 5
            onClicked: {
                Qt.quit()
            }
        }
    }
}
